package com.example.graduationdesignbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.example.graduationdesignbackend.constants.ResponseCode;
import com.example.graduationdesignbackend.entity.Info;
import com.example.graduationdesignbackend.exception.ParkException;
import com.example.graduationdesignbackend.mapper.InfoMapper;
import com.example.graduationdesignbackend.service.IInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.example.graduationdesignbackend.enums.InfoType;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuze
 * @since 2024-04-07
 */
@Service
public class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements IInfoService {

  @Override
  public Info getInfo(InfoType type) {
    Info info = lambdaQuery().eq(Info::getInfoType,type).one();
    if (BeanUtil.isEmpty(info)){
      info = new Info();
      info.setId(RandomUtil.randomNumbers(9));
      info.setInfoType(type.name());
      info.setInfoTypeName(type.text());
      info.setStatus(0);
      info.setCreateTime(new Date().getTime());
      info.setUpdateTime(new Date().getTime());
      Boolean save = save(info);
      if (!save) throw new ParkException(ResponseCode.SaveError);
    }
    return info;
  }

  @Override
  public void edit(Info info) {
    info.setUpdateTime(new Date().getTime());
    Boolean update = updateById(info);
    if (!update) throw new ParkException(ResponseCode.UpdateError);
  }
}
